<?php
namespace App\Service;

class StringOperate
{
    /**
     * 字符串截取，支持中文和其他编码
     * @param string $str 		需要转换的字符串
     * @param string $start 	开始位置
     * @param string $length 	截取长度
     * @param string $charset 	编码格式
     * @param string $suffix 	截断显示字符
     * @return string
     */
    function msubstr(string $str, int $start = 0, int $length = 1, bool $suffix = true, string $charset = "utf-8")
    {
        if($suffix && mb_strlen($str) > $length){
            return mb_substr($str, $start, $length, $charset) . "...";
        } else {
            return mb_substr($str, $start, $length, $charset);
        }
    }

    /**
     * 删除字符串中所有空格
     * @param string $str
     */
    function remmove_str_blank(string $str)
    {
        $blank = [" ", "　", "\t", "\n", "\r", "　", "&nbsp;", "&bsp;", "&dquo;", chr(10), chr(13)];
        $replace = ["", "", "", "", "", "", "", "", "", ""];
        return str_replace($blank, $replace, $str);
    }

    /**
     * 分割字符串返回数组，支持中文
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    function split_str(string $str)
    {
        return preg_split('/(?<!^)(?!$)/u', (string) $str);
    }

    /**
     * 隐藏字符串内容,支持中文
     * @param  [string]  $str   [description]
     * @param  integer $start [description]
     * @param  integer $size  [description]
     * @return [type]         [description]
     */
    function hidden_str(string $str, int $start = 0, int $size = 1, string $hide = '*')
    {
        $str = self::split_str($str);
        foreach ($str as $key => $value) {
            if($size > 0 && $key >= $start){
                $str[$key] = $hide;
                $size--;
            }
        }
        return implode("", $str);
    }

    /**
     * 字符串下划线转驼峰
     * @return [type] [description]
     */
    function under_line_to_hump(string $str)
    {
        if(false !== strpos($str, '_')){
            $str = explode('_', $str);
            foreach ($str as $key => $value) {
                $str[$key] = ucfirst($value);
            }
            return implode("", $str);
        }else{
            return ucfirst($str);
        }
    }

    /**
     * 获取随机字符串
     * @param      integer                  $length [description]
     * @param      [type]                   $type   [选择哪种字符串]
     * @param      [type]                   $canRepeat   [是否可重复字符串]
     * @return   string
     */
    function get_random_str(int $length = 4, int $type = 0, bool $canRepeat = true)
    {
        $types = [
            0 => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
            1 => '0123456789',
            2 => 'abcdefghijklmnopqrstuvwxyz',
            3 => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
            4 => '0123456789abcdefghijklmnopqrstuvwxyz',
            5 => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
            6 => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
        ];
        $resStr = '';
        // 选择方式
        $baseStr = $types[$type] ?? $types[0];
        // 循环获取随机字符串
        while ($length-- > 0) {
            // 打乱字符串
            $baseStr = str_shuffle($baseStr);
            // 获取长度
            $strLength = mb_strlen($baseStr);
            // 随机值
            $random = mt_rand(0, $strLength - 1);
            // 拼接结果
            $resStr .= $baseStr[$random];
            // 不允许重复，则删掉对应的字符
            if (false == $canRepeat) {
                $baseStr = str_replace($baseStr[$random], '', $baseStr);
            }
        }
        return $resStr;
    }

    /**
     * bc数字字符串 删除后面的0
     * @param      string                   $str [description]
     * @return     [type]                        [description]
     */
    function bc_str_del_behind_zero(string $str)
    {
        if (! is_int(strpos($str, '.'))) {
            return $str;
        }
        return rtrim(rtrim($str, '0'), '.');
    }

    /**
     * 数字不四舍五入 取小数点后面几位
     * @param    [type]                   $float [description]
     * @return   [type]                          [description]
     */
    function float_round($float, int $length)
    {
        if (! strpos($float, '.') && ! preg_match('/^[1-9]{0,}[0-9](\.[0-9]+)?$/', $float)) {
            return $float;
        }

        $res = explode('.', $float);
        $res[1] = substr($res[1], 0, $length);

        return "{$res[0]}.{$res[1]}";

    }
}